#!/usr/bin/env python
#
#====================
# arduino-webinlights - code.google.com/p/arduino-webinlights
# Developed by tommed (www.tommed.co.uk)
#
# MAIN PROGRAM
# ============
# This program monitors a log file and sends a "1" to the Arduino board via a serial connection (usb)
# which turns on another LED light.
#====================
import serial
import os
import sys
import time

# open serial connection to arduino
print "opening serial connection..."
s = serial.Serial('/dev/tty.usbserial-A900adIH', 9600)
print "serial connection open\n"

# file details
filepath = sys.argv[1] #'/var/log/apache2/access_log'
print "opening web log file "+filepath+"..."
filehandle = open(filepath, 'r')
print "web log file open\n"

# open file at end of the file
print "getting file stats..."
results = os.stat(filepath)
size = results[6]
filehandle.seek(size)
print "file stats opened\n"

while 1:
  where = filehandle.tell()
  line = filehandle.readline()
  if not line:
    time.sleep(1)
    filehandle.seek(where)
  else:
    s.write('1')
